/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import com.sun.lwuit.Command;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BoxLayout;
import core.IMAPMIDlet;
import core.PositionManager;
import core.ScreenEngine;
import view.additional.CellItem;
import view.additional.MenuList;
import view.commands.BackCommand;

/**
 *
 * @author Kvest
 */
public class EfirSettingsForm extends BasicForm implements MenuList.MenuListSelection
{
    private static final int OK_COMMAND_ID = 1;
    
    private static final int SORT_ITEM_ID = 0;
    private static final int REGION_FILTER_ITEM_ID = 1;
    
    private SortSettingsForm sortSettingsForm;
    private RegionsFilterForm regionsFilterForm = new RegionsFilterForm();
    
    private MenuList items;
    private CellItem sortItem;
    
    public EfirSettingsForm()
    {
        super("efir_settings");
        
        init();
    }
    
    private void init()
    {
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        setScrollable(false);
        
        items = new MenuList();
        sortItem = new CellItem(SORT_ITEM_ID, "sort");
        items.addItem(sortItem);
        items.addItem(new CellItem(REGION_FILTER_ITEM_ID, "region_filter"));
        items.setSelectionListener(this);
        addComponent(items);
        
        //Команды
        addCommand(new BackCommand());
        
        Command ok = new Command("ok", OK_COMMAND_ID)
        {
            public void actionPerformed(ActionEvent evt)
            {
                itemSelected(items.getSelectedId());
                
                evt.consume();
            }
        };
        addCommand(ok);
    }
    
    public void show()
    {
        //Устанавливаем активные\неактивные пункты
        setItemsState();
        
        super.show();
    }
    
    private void setItemsState()
    {
        sortItem.setEnabled(state == CONNECTED_STATE);
    }
    
    public void itemSelected(int id)
    {
        switch(id)
        {
            case SORT_ITEM_ID : showSortSettingsForm(); break;
            case REGION_FILTER_ITEM_ID : showRegionsFilter(); break;
        }
    }
    
    private void showSortSettingsForm()
    {
        if (sortSettingsForm == null)
        {
            sortSettingsForm = new SortSettingsForm();
        }
        
        ScreenEngine.getEngine().push(sortSettingsForm);
    }
    
    private void showRegionsFilter()
    {
         //Проверяем наличие стоянок и коллбэком ставим обновление форму регионов
        IMAPMIDlet.getMidlet().getPositionManager().checkForPositionUpdates(new Runnable() 
        {
            public void run() 
            {
                //Обновляем форму отображения стоянок
                if (regionsFilterForm != null)
                {
                    regionsFilterForm.setContent();
                    regionsFilterForm.repaint();
                }
            }
        }, 
        new Runnable() 
        {
            public void run() 
            {
                //Просто принудительно перерисовываем форму - все остальное сделает сама форма
                if (regionsFilterForm != null && regionsFilterForm.isVisible())
                {
                    regionsFilterForm.repaint();
                }
            }
        });
        
        //Отображаем форму с регионами
        showRegionsFilterForm();
    }
    
    private void showRegionsFilterForm()
    {
        if (regionsFilterForm == null)
        {
            regionsFilterForm = new RegionsFilterForm();
        }
        
        ScreenEngine.getEngine().push(regionsFilterForm);
    }
}
